<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="library/layui/css/layui.css">
    <link rel="stylesheet" href="report.css">
    <title>试压报告</title>
</head>

<body>
    <div class="layui-container">
        <div id="report-content">
            <div class="layui-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 title">
                    YGBG-43
                </div>
                <div class="layui-col-xs10 layui-col-sm10 layui-col-md10 title last-right">
                    <h2>管道试压报告</h2>
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    单位工程名称
                </div>
                <div id="project_name" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content">
                    --
                </div>
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    工程编号
                </div>
                <div id="project_number" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content last-right">
                    --
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    桩号、区间
                </div>
                <div id="stake_mark" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content">
                    --
                </div>
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    管材规格
                </div>
                <div id="pipe_specification" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content last-right">
                    --
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    设计压力
                </div>
                <div id="design_pressure" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content">
                    --
                </div>
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    管道长度
                </div>
                <div id="pipe_length" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content last-right">
                    --
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    试压介质
                </div>
                <div id="medium" class="layui-col-xs4 layui-col-sm4 layui-col-md4 content">
                    --
                </div>
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    图纸档案号NO.
                </div>
                <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 content last-right">
                    --
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    试压起止时间
                </div>
                <div id="date" class="layui-col-xs10 layui-col-sm10 layui-col-md10 content last-right">
                    --
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 stage-title">
                    强度试压
                </div>
                <div id="date" class="layui-col-xs10 layui-col-sm10 layui-col-md10 stage-content">
                    <div class="layui-row">
                        <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item">压力（MPa）</div>
                        <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item">稳压时间</div>
                        <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item">压降</div>
                    </div>
                    <div class="layui-row">
                        <div id="intensity_pressure" class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item-content">--</div>
                        <div id="intensity_time" class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item-content">--</div>
                        <div id="intensity_difference"  class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item-content">--</div>
                    </div>
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 stage-title">
                    严密性试压
                </div>
                <div id="date" class="layui-col-xs10 layui-col-sm10 layui-col-md10 stage-content last-right">
                    <div class="layui-row">
                        <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item">压力（MPa）</div>
                        <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item">稳压时间</div>
                        <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item">压降</div>
                    </div>
                    <div class="layui-row">
                        <div id="rigor_pressure"  class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item-content">--</div>
                        <div id="rigor_time"  class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item-content">--</div>
                        <div id="rigor_difference"  class="layui-col-xs4 layui-col-sm4 layui-col-md4 stage-item-content">--</div>
                    </div>
                </div>
            </div>
            <div class="layui-row report-row">
                <div class="layui-col-xs2 layui-col-sm2 layui-col-md2 content">
                    试压结论
                </div>
                <div id="result" class="layui-col-xs10 layui-col-sm10 layui-col-md10 content last-right">
                    --
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 signature">
                    <div class="layui-row signature-row">
                        试压负责人：
                    </div>
                    <div class="layui-row signature-row">
                        日期：
                    </div>
                </div>
                <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 signature">
                    <div class="layui-row signature-row">
                        监理单位：
                    </div>
                    <div class="layui-row signature-row">
                        日期：
                    </div>
                </div>
                <div class="layui-col-xs4 layui-col-sm4 layui-col-md4 signature last-right">
                    <div class="layui-row signature-row">
                        建设单位：
                    </div>
                    <div class="layui-row signature-row">
                        日期：
                    </div>
                </div>
            </div>
            <div style="height: .6rem;"></div>
            <div class="layui-row">
                <div class="layui-row">
                    <div class="layui-col-xs12 layui-col-sm12 layui-col-md12" style="text-align: center">
                        <div class="content-title">试压曲线</div>
                        <div style="background-color: #c2c2c2; padding: .1rem;">
                            <img id="chart_image" src="" style="width: 99%">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
                <div id="result" class="content-title">
                    <button id="export" type="button" class="layui-btn">导出</button>
                </div>
            </div>
        </div>
        <div class="layui-row layui-hide">
            <canvas id="myCanvas"></canvas>
        </div>
    </div>
    <script src="library/jquery/jquery-3.7.1.min.js"></script>
    <script src="library/layui/layui.js"></script>
    <script src="library/jspdf/jspdf.min.js"></script>
    <script src="library/moment/moment.js"></script>
    <script src="library/html2canvas/html2canvas.min.js"></script>
    <script>
        var c = document.getElementById("myCanvas");
        var ctx = c.getContext("2d");
        $("#export").on('click', function () {
            layerLoading("正在生成试压报告...");
            html2canvas(document.getElementById('report-content'), {
                dpi: window.devicePixelRatio * 4, // 将分辨率提高到特定的DPI 提高四倍
                scale: 4 // 按比例增加分辨率
            }).then(function (canvas) {
                savePDF(canvas);
            });
        })

        function getBase64Image(url, ref) {
            var image = new Image();
            image.src = url
            image.onload = function () {
                var base64 = drawBase64Image(image);
                ref.attr("src", url);
            };
        }
        function drawBase64Image(img) {
            var canvas = document.createElement("canvas");
            canvas.width = img.width;
            canvas.height = img.height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width, img.height);
            var dataURL = canvas.toDataURL("image/png");
            return dataURL;
        }
        function savePDF(canvas) {
            document.body.appendChild(canvas);
            //savePDF(canvas);
            //var pdf = new jsPDF('l', 'mm', 'a3') // A3纸，横向
            var pdf = new jsPDF('l', 'mm', 'a4') // A4纸，纵向
            var ctx = canvas.getContext('2d')
            var a4w = 190; var a4h = 277 // A4大小，210mm x 297mm，四边各保留10mm的边距，显示区域190x277
            //var a4w = 259; var a4h = 400 // A3大小，279mm x 420mm，四边各保留10mm的边距，显示区域259x400
            // var imgHeight = Math.floor(a4h * canvas.width / a4w) // 按A4显示比例换算一页图像的像素高度[这个计算方式应该是纵向]
            var imgHeight = Math.floor(a4w * canvas.width / a4h) // 按A4显示比例换算一页图像的像素高度[这个是横向打印的计算方法]
            var renderedHeight = 0

            while (renderedHeight < canvas.height) {
                var page = document.createElement('canvas')
                page.width = canvas.width
                page.height = Math.min(imgHeight, canvas.height - renderedHeight)// 可能内容不足一页

                // console.log(canvas.width, Math.min(imgHeight, canvas.height - renderedHeight))
                // console.log(a4w, Math.min(a4h, a4w * page.height / page.width))

                // 用getImageData剪裁指定区域，并画到前面创建的canvas对象中
                page.getContext('2d').putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0)
                //pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width)) // 添加图像到页面，保留10mm边距 [a4使用]
                pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w * 1.414, Math.min(a4h, a4w * page.height / page.width) * 1.414) // 添加图像到页面，保留10mm边距[a3使用1.414换算比例]

                renderedHeight += imgHeight
                if (renderedHeight < canvas.height) {
                    pdf.addPage()// 如果后面还有内容，添加一个空页
                }
                // delete page;
            }
            var now = moment().format("YYYY-MM-DD");
            var title = now + "-" + $("#project_name").text() + "-" + $("#project_number").text() + '.pdf';
            console.log($("#project_name"))
            // 保存文件
            pdf.save(title)
            endLayerLoading();
        }
        //更新信息
        function updateInfo(id, msg) {
            $(id).text(msg);
        }
        var imageData;
        //更新图片
        function updateImage(content) {
            imageData = content;
            getBase64Image(imageData, $('#chart_image'));
        }

        //loading
        var loadIndex;
        function layerLoading(msg) {
            loadIndex = layer.msg(msg, { icon: 16, shade: 0.3, time: 0 });
        }
        //loading结束
        function endLayerLoading() {
            layer.close(loadIndex);
        }
    </script>
</body>

</html>